package com.spring.sunflower.conversation

import android.content.Context
import android.graphics.Color
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseFragment
import com.spring.sunflower.bean.TalkRepositoryBean
import com.spring.sunflower.common.RelationAdapter
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * Created by Kevin on 2021/7/30<br/>
 *
 * Describe:<br/>
 */
class TopicFragment : BaseFragment<TopicPresenter>(), ConversationContract.TopicView {
    private var magicIndicator: MagicIndicator? = null
    private var mViewPager: ViewPager? = null
    private var mAdapter: RelationAdapter? = null
    private var titleList: MutableList<String> = ArrayList()
    private var mFragments: MutableList<Fragment> = ArrayList()
    override fun setLayoutResId(): Int = R.layout.fragment_topic

    override fun initView() {
        magicIndicator = mView.findViewById(R.id.magicIndicator)
        mViewPager = mView.findViewById(R.id.viewpager)
        mAdapter = RelationAdapter(childFragmentManager, titleList, mFragments)
        mViewPager?.let {
            it.adapter = mAdapter
        }
        mPresenter.loadTopicRepository()
    }

    override fun initData() {
    }

    override fun initListener() {
    }

    override fun loadData() {
    }

    override fun setPresenter(): TopicPresenter = TopicPresenter(this)
    override fun showTopic(data: List<TalkRepositoryBean.DatasBean>) {
        var titleData: MutableList<String> = ArrayList()
        var fragmentData: MutableList<Fragment> = ArrayList()
        for (d in data) {
            titleData.add(d.tag)
            val element = TopicSubFragment.newInstance(d.contentArray)
            fragmentData.add(element)
        }
        mAdapter?.updateData(titleData, fragmentData)
        initIndicator()
    }

    private fun initIndicator() {
        val commonNavigator = CommonNavigator(mActivity)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return titleList.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView =
                    ColorTransitionPagerTitleView(context)
                simplePagerTitleView.text = titleList[index]
                simplePagerTitleView.normalColor = Color.parseColor("#9e9e9e")
                simplePagerTitleView.selectedColor = Color.parseColor("#000000")
                simplePagerTitleView.setOnClickListener { mViewPager!!.currentItem = index }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
//                val indicator = LinePagerIndicator(context)
//                indicator.mode = LinePagerIndicator.MODE_EXACTLY
//                indicator.lineHeight = UIUtil.dip2px(context, 6.0).toFloat()
//                indicator.lineWidth = UIUtil.dip2px(context, 10.0).toFloat()
//                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
//                indicator.startInterpolator = AccelerateInterpolator()
//                indicator.endInterpolator = DecelerateInterpolator(2.0f)
//                indicator.setColors(Color.parseColor("#00c853"))
                return null
            }
        }
        magicIndicator!!.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, mViewPager)
    }

}